add_library(fgt_SDK SHARED IMPORTED)

if(${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})
	string(TOLOWER ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID} TARGET_ARCHITECTURE)
else()
	string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} TARGET_ARCHITECTURE)
endif()

set(ARCH_IS_X86 FALSE)
set(ARCH_IS_ARM FALSE)
set(ARCH_IS_64BITS FALSE)
if(${TARGET_ARCHITECTURE} MATCHES "(x|amd)(86|64)")
	set(ARCH_IS_X86 TRUE)
endif()
if(${TARGET_ARCHITECTURE} MATCHES "(arm|aarch)")
	set(ARCH_IS_ARM TRUE)
endif()
if(${CMAKE_SIZEOF_VOID_P} MATCHES "8")
	set(ARCH_IS_64BITS TRUE)
endif()

if(WIN32)
	if(${ARCH_IS_X86} AND NOT ${ARCH_IS_64BITS})
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlls/windows/x86/fgt_SDK.dll)
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_IMPLIB ${CMAKE_CURRENT_SOURCE_DIR}/dlls/windows/x86/fgt_SDK.lib)
	elseif(${ARCH_IS_X86} AND ${ARCH_IS_64BITS})
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlls/windows/x64/fgt_SDK.dll)
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_IMPLIB ${CMAKE_CURRENT_SOURCE_DIR}/dlls/windows/x64/fgt_SDK.lib)
	else()
		message(FATAL_ERROR "Architecture ${TARGET_ARCHITECTURE} not supported on Windows")
	endif()

elseif(UNIX AND NOT APPLE)
	 if(${ARCH_IS_X86} AND ${ARCH_IS_64BITS})
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlls/linux/x64/libfgt_SDK.so)
	elseif(${ARCH_IS_ARM} AND NOT ${ARCH_IS_64BITS})
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlls/linux/arm/libfgt_SDK.so)
	elseif(${ARCH_IS_ARM} AND ${ARCH_IS_64BITS})
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlls/linux/arm64/libfgt_SDK.so)
	else()
		message(FATAL_ERROR "Architecture ${TARGET_ARCHITECTURE} not supported on Linux")
	endif()

elseif(APPLE)
	if(${ARCH_IS_X86} AND ${ARCH_IS_64BITS})
		set_property(TARGET fgt_SDK PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlls/mac/x64/libfgt_SDK.dylib)
	else()
		message(FATAL_ERROR "Architecture ${TARGET_ARCHITECTURE} not supported on macOS")
	endif()
else()
	message(FATAL_ERROR "OS ${CMAKE_SYSTEM_NAME} not supported")
endif()

get_property(SDK_LIBRARY_PATH TARGET fgt_SDK PROPERTY IMPORTED_LOCATION)
set(SDK_LIBRARY_PATH ${SDK_LIBRARY_PATH} PARENT_SCOPE)

function(copy_sdk_lib project_name)
	add_custom_command(TARGET ${project_name} POST_BUILD
					   COMMAND ${CMAKE_COMMAND} -E copy
						  ${SDK_LIBRARY_PATH} $<TARGET_FILE_DIR:${project_name}>)
endfunction()

add_library(fgt_SDK_cpp INTERFACE)
target_sources(fgt_SDK_cpp INTERFACE fgt_SDK_Cpp.cpp)
target_include_directories(fgt_SDK_cpp INTERFACE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/dlls)
target_link_libraries(fgt_SDK_cpp INTERFACE fgt_SDK)